package com.example.wordapp.service.impl;

import com.example.wordapp.mapper.UserMapper;
import com.example.wordapp.mapper.WordMapper;
import com.example.wordapp.pojo.User;
import com.example.wordapp.pojo.Word;
import com.example.wordapp.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class WordServiceImp implements WordService {

    @Autowired
    private WordMapper wordMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Word> getAll() {
        return wordMapper.getAll();
    }

    @Override
    public List<Word> getByUserFormart(User user) {
        // 获取是第几个用户
        Integer id = user.getId();
        // 拼接字符串
        String formart = "user_" + id;
        //调用查询
        return wordMapper.getByUser(formart);
    }

    @Override
    public int updateWriteWord(String userId, String wordId, Integer stat) {
        // 拼接字符串
        String formart = "user_" + userId;
        return wordMapper.updateWriteWord(formart, wordId, stat);
    }

    @Override
    public List<Word> findWriteWordByCom(String userId, Integer isWrite) {
        // 拼接字符串
        String formart = "user_" + userId;
        return wordMapper.findWriteWordByCom(formart, isWrite);
    }

    @Override
    public int cleanWriterWords(String userId, String code) {
        // 先验证用户是不是被授权的
        User user = userMapper.login(Integer.parseInt(userId), code);
        if (user == null) {
            return -1;
        }
        // 用户有效 拼接字符串
        String formart = "user_" + userId;
        return wordMapper.cleanWriterWords(formart);
    }
}
